#pragma once 

#include <iostream>
#include <string>
#include <set>

class Command
{
public:
    Command()
    {
        _list.insert("ls");
        _list.insert("ls -l");
        _list.insert("pwd");
        _list.insert("touch");
        _list.insert("whoami");
        _list.insert("ll");
    }

    bool Check(const std::string& str)
    {
        auto it = _list.find(str);
        return it == _list.end() ? false : true;
    }

    std::string Execute(const std::string& cmd)
    { 
        // 1.pipe
        // 2.fork + dup2(pipe[1], 1) + exec* 
        if (!Check(cmd))
        {
            return cmd + "不支持";
        }
        FILE* fp = popen(cmd.c_str(), "r");
        if (fp == nullptr)
        {
            return "Failed";
        }
        char buffer[1024];
        std::string result;
        while (true)
        {
            char *ret = fgets(buffer, sizeof(buffer), fp);
            if (!ret) break;
            result += ret;
        }
        pclose(fp);
        return result.empty() ? "Done" : result;
    }
    
    ~Command()
    {}
private:
    std::set<std::string> _list;
};